package org.nicholasjones.radiocontroller.comms;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.logging.Logger;

import gnu.io.*;

/**
 * Created with IntelliJ IDEA.
 * User: Nicholas Jones
 * Date: 6/3/13
 * Time: 9:09 PM
 * To change this template use File | Settings | File Templates.
 */
public class SerialCommPort {

    private static final Logger LOG = Logger.getLogger(SerialCommPort.class.getName());

    private String portName;
    private CommPortIdentifier portIdentifier;
    private SerialPort serialPort;

    public SerialCommPort(String name) {
        portName = name;
    }

    public String getPortName() {
        return portName;
    }

    public void connect() throws CommsException {

        try {
            portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
            if (portIdentifier.isCurrentlyOwned()) {
                CommsException ce = new CommsException("ALREADYOWNED", "Serial port already owned");
                throw ce;
            }

            CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
            if ( commPort instanceof SerialPort )
            {
                serialPort = (SerialPort) commPort;
                serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
            }
            else {
                System.out.println("Error: Only serial ports are handled by this example.");
            }


        } catch (NoSuchPortException e) {
            CommsException ce = new CommsException("NOSUCHPORT", "No such port exception");
            throw ce;
        } catch (PortInUseException e) {
            CommsException ce = new CommsException("PORTINUSE", "Port in use");
            throw ce;
        } catch (UnsupportedCommOperationException e) {
            CommsException ce = new CommsException("UNSUPPORTEDOP", "Unsupported Comm Operation");
            throw ce;
        }
        /*
        catch (IOException e) {
            CommsException ce = new CommsException("IOEXCEPTION", "IOException encountered");
            throw ce;
        }
        */

    }

    static public List<String> getPortIdentifierList() {

        List<String> portIdentififiers = new ArrayList<String>();
        Enumeration<CommPortIdentifier> portIdentifiers = CommPortIdentifier.getPortIdentifiers();

        while (portIdentifiers.hasMoreElements()) {
            CommPortIdentifier pid = portIdentifiers.nextElement();
            portIdentififiers.add(pid.getName());
            LOG.info("getPortIdentifierList retrieved port " + pid.getName());
        }
        return portIdentififiers;
    }

    public void send(String message) throws CommsException {

        try {
            if (null != serialPort) {
                OutputStream output = serialPort.getOutputStream();
                output.write(message.getBytes());
            }
        } catch (IOException e) {
             CommsException ce = new CommsException("IOEXCEPTION", "IOException encountered");
             throw ce;
        }
    }
}